<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Models\UserRecharge;
use App\Traits\Controllers\DestroyTraits;
use App\Traits\Controllers\IndexTraits;
use App\Transformers\User\UserRechargesTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;

class UserRechargesController extends Controller
{
    use IndexTraits;

    //删除
    use DestroyTraits;

    public $model = UserRecharge::class;

    public $transformer = UserRechargesTransformer::class;

    public $time;
    public $start;
    public $stop;

    /**
     * 充值记录列表
     */

    public function index(Request $request)
    {
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        $this->formatIndexQuery($request);
        //获取传过来的时间类型
        $this->time = $request->input('time');
        //获取开始时间和结束时间
        $this->start = $request->input('start');
        $this->stop = $request->input('stop');
        //获取用户昵称
        $name = $request->input('user_name');


        $data = $this->aa($this->time, $this->start, $this->stop)
            ->whereHas('user', function ($query) use ($name) {
               if(!empty($name)){
                   $query->where('nickname',$name);
               }
            })
            ->pimp()//自动转换where条件
            ->paginate($this->getPageLimit());

        return $this->response->paginator($data, resolve($this->transformer));
    }

    /**
     * 充值列表
     */
    public function totalPrice(Request $request)
    {
        //获取传过来的时间类型
        $this->time = $request->input('time');
        //获取开始时间和结束时间
        $this->start = $request->input('start');
        $this->stop = $request->input('stop');
        //获取用户昵称
        $name = $request->input('user_name');

        $search = $this->aa($this->time, $this->start, $this->stop)
            ->whereHas('user', function ($query) use ($name) {
                if(!empty($name)){
                    $query->where('nickname',$name);
                }
            })
            ->pimp()->get();
        $refund_price = $officialAccount = $applet = $price = 0;
        foreach ($search as $k => $v) {
            $refund_price += $v->refund_price;//充值退款金额

            //公众号充值金额
            if ($v->recharge_type == 'officialAccount') {
                $officialAccount += $v->officialAccount;
            }

            //小程序充值金额
            if ($v->recharge_type == 'applet') {
                $applet += $v->applet;
            }
            //充值总金额
            $price += $v->price;
        }
        $arr = [];
        $arr['refund_price'] = $refund_price;
        $arr['price'] = $price;
        $arr['officialAccount'] = $officialAccount;
        $arr['applet'] = $applet;


        return $this->response->array($arr);
    }

    public function aa($time, $start, $stop)
    {
        return $this
            ->getQuery()->when($time || $start || $stop, function ($query) use ($time, $start, $stop) {
                //自定义时间
                if (!empty($start) && !empty($stop)) {
                    $query->whereBetween('created_at', [$start, $stop]);
                }

                //搜索今天数据
                if ($time == "today") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()]);
                }

                //搜索昨天数据
                if ($time == "yesterday") {
                    $query->whereDay('created_at', [Carbon::now()->yesterday(), Carbon::now()->startOfDay()]);
                }

                //搜索最近一周数据
                if ($time == "week") {
                    $query->whereBetween('created_at', [Carbon::now()->subDays(7), Carbon::now()->subDays(0)]);
                }

                //搜索最近三十天数据
                if ($time == "thirty") {
                    $query->whereBetween('created_at', [Carbon::now()->subDays(30), Carbon::now()->subDays(0)]);
                }

                //搜索本月数据
                if ($time == "month") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()]);
                }

                //搜索本年数据
                if ($time == "year") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()]);
                }
            });
    }
}
